﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PFMConfigurations;
using PFMGlobals;
using System.Collections;

namespace PFMKernels
{
    public class PFMTaskDispatcher
    {
        #region 基本功能
        //没有对象池 
        public virtual bool DoSpecifiedTask(PFMConfigurationManager aConfigurationsManager, Target aTarget)
        {
            ITask task = PFMTaskFactory.CreateTask(PFMDefinitions.SPECIFIEDTASK);
            return task.Execute(null, aTarget);
        }

        public virtual bool DoScheduledTask(PFMConfigurationManager o_ConfigurationsManager, List<Schedule> schedulesList)
        {
            bool result = true;
            ITask task = PFMTaskFactory.CreateTask(PFMDefinitions.SCHEDULEDTASK);
            try
            {
                foreach (var s in schedulesList)
                {
                    Target t = o_ConfigurationsManager.GetTargetFromSchedule(s);
                    if(!task.Execute(s, t))
                        result = false ;
                }
            }
            catch { result = false; }
            return result;
        }
        #endregion
        //#region 添加对象池
        //private PFMTaskManager  o_PFMTaskManager = null;

        //public PFMTaskDispatcher()
        //{
        //    o_PFMTaskManager = new PFMTaskManager();
        //}

        //~PFMTaskDispatcher()
        //{
        //    o_PFMTaskManager = null;
        //}

        //public PFMTaskDispatcher(int MinPoolSize, int MaxPoolSize)
        //{
        //    o_PFMTaskManager = new PFMTaskManager(MinPoolSize, MaxPoolSize);
        //}
        //public virtual bool DoSpecifiedTask(PFMConfigurationManager aPFMManager, Target aTarget)
        //{
        //    PFMTask aSpecifiedTask = null;
        //    bool bResult = true;

        //    try
        //    {
        //        aSpecifiedTask = o_PFMTaskManager.GetSpecifiedTask();
        //        bResult = aSpecifiedTask.Execute(null, aTarget);
        //    }
        //    catch
        //    {
        //        bResult = false;
        //    }
        //    finally
        //    {
        //        o_PFMTaskManager.ReleaseTaskObject(aSpecifiedTask);
        //    }

        //    return bResult;
        //}

        //public virtual bool DoScheduledTask(PFMConfigurationManager aPFMManager, List<Schedule> arSchedules)
        //{
        //    Schedule aSchedule = null;
        //    Target aTarget = null;
        //    PFMTask aScheduledTask = null;
        //    bool bResult = true;

        //    try
        //    {
        //        IEnumerator ie = arSchedules.GetEnumerator();
        //        aScheduledTask = o_PFMTaskManager.GetScheduledTask();
        //        while (ie.MoveNext())
        //        {
        //            aSchedule = ie.Current as Schedule;
        //            aTarget = aPFMManager.GetTargetFromSchedule(aSchedule);
        //            bResult = aScheduledTask.Execute(aSchedule, aTarget);
        //        }
        //    }
        //    catch
        //    {
        //        bResult = false;
        //    }
        //    finally
        //    {
        //        o_PFMTaskManager.ReleaseTaskObject(aScheduledTask);
        //    }

        //    return bResult;
        //}

        //public PFMTaskManager TaskManager
        //{
        //    get
        //    {
        //        return o_PFMTaskManager;
        //    }
        //}

        //#endregion
    }
}
